﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reviewer.Entities.Services;
using Reviewer.Data;
using System.Collections.ObjectModel;
using Reviewer.Entities;
using System.Data;
using System.Web;
using System.Globalization;

namespace Reviewer.Business.Services
{
    internal class GameViewService : BaseService, IGameViewService
    {
        internal GameViewService() : base() { }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }


        public Collection<GameView> FindTopBy(DateTime forDate, int topCount)
        {
            return new GameViewRepository().SelectTopByDate(forDate, topCount);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public void LogView(int gameId, Guid gameUid)
        {
            try
            {
                new GameViewRepository().AddGameView(gameId, gameUid);
            }
            catch (Exception err)
            {
                System.Diagnostics.Trace.TraceError(err.ToString());
            }
        }

        public DataSet CompileViewStatistics()
        {
            try
            {
                return new GameViewRepository().GetViewStatistics(null, null);
            }
            catch (Exception err)
            {
                System.Diagnostics.Trace.TraceError(err.ToString());
            }

            return null;
        }

        public DataSet CompileViewStatisticsByGenre(Int32 genreId)
        {
            return new GameViewRepository().GetViews();
            //try
            //{
            //    if (HttpContext.Current.Cache["CompileViewStatisticsG" + genreId.ToString()] == null)
            //    {
            //        DataSet set = new GameViewRepository().GetViewStatistics(null, genreId);
            //        //Add with expires every hour
            //        HttpContext.Current.Cache.Add("CompileViewStatisticsG" + genreId.ToString(), set, null, DateTime.Now.AddMinutes(60 - DateTime.Now.Minute + 1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
            //        return set;
            //    }

            //    return HttpContext.Current.Cache["CompileViewStatisticsG" + genreId.ToString()] as DataSet;
            //}
            //catch (Exception err)
            //{
            //    System.Diagnostics.Trace.TraceError(err.ToString());
            //}

            //return null;
        }

        public DataSet CompileViewStatisticsByFormat(Int32 formatId)
        {
            try
            {
                if (HttpContext.Current.Cache["CompileViewStatisticsF" + formatId.ToString(CultureInfo.InvariantCulture)] == null)
                {
                    DataSet set = new GameViewRepository().GetViewStatistics(formatId, null);
                    //Add with expires every hour
                    HttpContext.Current.Cache.Add("CompileViewStatisticsF" + formatId.ToString(CultureInfo.InvariantCulture), set, null, DateTime.Now.AddMinutes(60 - DateTime.Now.Minute + 1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
                    return set;
                }

                return HttpContext.Current.Cache["CompileViewStatisticsF" + formatId.ToString(CultureInfo.InvariantCulture)] as DataSet;
            }
            catch (Exception err)
            {
                System.Diagnostics.Trace.TraceError(err.ToString());
            }

            return null;
        }
    }
}
